'''
quotestochar.py

Copyright 2009 Xavier Mendez Navarro aka Javi

This file is part of pysqlin

pysqlin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

pysqlin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with pysqlin; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'''

from framework.interfaces import IFilter
from framework.baseclass import BFilter
import re

class QuotesToChar(BFilter, IFilter):
    def name(self): return 'QuotesToChar'
    def summary(self): return "Replaces 'xxx' with char(0xFF,0xFF...)."
    def priority(self): return 89
    def database(self): return ('mysql',)
    def type(self): return (IFilter.DB_QUERY,)
    def transform(self, request):
	p = re.compile("('(.*?)')")

	for quoted, inside in p.findall(request):
	    charred =  'char(' + ','.join(map(lambda x:hex(ord(x)), inside)) + ')'
	    request = request.replace(quoted, charred)

	return request

def load(api):
    return QuotesToChar()
